package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.entity.IndexInfo;
import work.mediway.mdm.core.entity.Table;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.core.transport.ErrorMsgVO;
import work.mediway.mdm.biz.dto.dict.CodeSystemDTO;
import work.mediway.mdm.biz.service.biz.TableModelService;
import work.mediway.mdm.biz.service.dict.CtCodeSystemService;
import work.mediway.mdm.biz.vo.dict.CtCodeSystemVO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2022/6/30
 */
@RestController
@RequestMapping("model")
@Validated
@Slf4j
@Api(tags = "表模型管理模块")
@ApiSupport(order = 24)
@RequiredArgsConstructor
public class TableModelController {

    private final CtCodeSystemService codeSystemService;
    private final TableModelService tableModelService;

    @OpsLog(value = "创建数据库表", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "创建数据库表", content = "创建数据库表")
    @ApiOperation("创建数据库表")
    @ApiOperationSupport(order = 1)
    @PostMapping("table")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<String> createTable(@RequestBody @Valid Table table) {
        this.tableModelService.createTable(table);
        return BaseResponse.success();
    }

    @OpsLog(value = "获取表模型列表", type = OpsLogTypeEnum.SELECT)
    @ApiOperation("获取表模型列表")
    @ApiOperationSupport(order = 3)
    @GetMapping
    public BaseResponse<Page<CtCodeSystemVO>> selectAllTable(CodeSystemDTO codeSystem, QueryRequest queryRequest) {
        List<CtCodeSystemVO> ctCodeSystemVoList = this.codeSystemService.selectAllTable(codeSystem, queryRequest);
        Page<CtCodeSystemVO> page = new Page<>();
        page.setRecords(ctCodeSystemVoList);
        page.setSize(ctCodeSystemVoList.size());
        return BaseResponse.success(page);
    }

    @OpsLog(value = "获取表模型详情", type = OpsLogTypeEnum.SELECT)
    @ApiOperation("获取表模型详情")
    @ApiOperationSupport(order = 4)
    @GetMapping("/table/{tableName}")
    public BaseResponse<Table> selectTable(@PathVariable("tableName") String tableName) {
        Table table = this.tableModelService.selectTable(tableName);
        return BaseResponse.success(table);
    }

    @OpsLog(value = "更新数据库表", type = OpsLogTypeEnum.UPDATE)
    @OperLog(title = "更新数据库表", content = "更新数据库表")
    @ApiOperation("更新数据库表")
    @ApiOperationSupport(order = 5)
    @PostMapping("table/update")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<String> updateTable(@RequestBody Table table) throws SQLException {
        this.tableModelService.updateTable(table);
        return BaseResponse.success();
    }

    @OpsLog(value = "删除数据库表", type = OpsLogTypeEnum.DELETE)
    @OperLog(title = "删除数据库表", content = "删除数据库表")
    @ApiOperation("删除数据库表")
    @ApiOperationSupport(order = 7)
    @PostMapping("table/drop")
    public BaseResponse<ErrorMsgVO> deleteTable(@RequestBody List<String> tableName) {
        return BaseResponse.success(this.tableModelService.deleteTable(tableName));
    }

    @OpsLog(value = "获取表索引", type = OpsLogTypeEnum.SELECT)
    @OperLog(title = "获取表索引", content = "获取表索引")
    @ApiOperation("获取表索引")
    @ApiOperationSupport(order = 9)
    @GetMapping("table/index")
    public BaseResponse<List<IndexInfo>> selectTableIndexInfo(@NotBlank(message = "不能为空") String tableName) {
        List<IndexInfo> indexInfos = this.tableModelService.selectTableIndexInfo(tableName);
        return BaseResponse.success(indexInfos);
    }

    @OpsLog(value = "新增表索引", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "新增表索引", content = "新增表索引")
    @ApiOperation("新增表索引")
    @ApiOperationSupport(order = 11)
    @PostMapping("table/index")
    public BaseResponse<ErrorMsgVO> saveTableIndexInfo(@RequestBody @Valid List<IndexInfo> indexInfos) {
        ErrorMsgVO msgVO = this.tableModelService.saveTableIndexInfo(indexInfos);
        return BaseResponse.success(msgVO);
    }

    @OpsLog(value = "删除表索引", type = OpsLogTypeEnum.DELETE)
    @OperLog(title = "删除表索引", content = "删除表索引")
    @ApiOperation("删除表索引")
    @ApiOperationSupport(order = 13)
    @PostMapping("table/index/delete")
    public BaseResponse<ErrorMsgVO> deleteTableIndexInfo(@RequestBody @Valid IndexInfo.IndexInfoDTO indexInfoDTO) {
        ErrorMsgVO msgVO = this.tableModelService.deleteTableIndexInfo(indexInfoDTO);
        return BaseResponse.success(msgVO);
    }

}











